package org.cttic.visualization.visualization.util;

import org.cttic.visualization.visualization.common.esCommon;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.cttic.visualization.visualization.util.itineraryQuery.queryUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-25 9:38
 */
@Component
public class VehicleInformationUtil {
    @Autowired
    private RestHighLevelClient restHighLevelClient;


    public void informationQuery(String vehiclePlate,String vehicleColor,sectionBillQuery sectionBillQuery){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("vehiclePlate",vehiclePlate));
//        boolQueryBuilder.must(QueryBuilders.termQuery("vehicleColor",vehicleColor));
        boolQueryBuilder.must(QueryBuilders.termQuery("from","2"));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));

        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.sort("tansTime",SortOrder.DESC);
        searchSourceBuilder.size(1);

        // 执行搜索,向ES发起http请求
        SearchRequest Request = new SearchRequest(esCommon.access_info_202004_test,esCommon.access_info_202005_test,esCommon.esLwzxIndex__202005);
        Request.types("_doc");

        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {
            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SearchHit[] hits = rp.getHits().getHits();
        /*
        "tansTime" : "2020-05-01 05:32:19",     时间
        "pointNo" : "S0020330010050",   出口编号
        "enExFlag" : "2",       出入口
        "mediaType" : 1,        通行类型
        "vehicleType" : 13,     车型
        "vehicleClass" : 24,    车种
        "axleCount" : 3,        轴数
         */
        if(hits.length>0){
            SearchHit hit = hits[0];
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            Object vehicleType = sourceAsMap.get("vehicleType");
            Object enExFlag = sourceAsMap.get("enExFlag");
            Object pointNo = sourceAsMap.get("pointNo");
            Object mediaType = sourceAsMap.get("mediaType");
            Object vehicleClass = sourceAsMap.get("vehicleClass");
            Object axleCount = sourceAsMap.get("axleCount");
            //设置车型，通行类型，车种，轴数
            if(vehicleType!=null){
                sectionBillQuery.setVehCode(Integer.parseInt(String.valueOf(vehicleType)));
                System.out.println("vehicleType:"+Integer.parseInt(String.valueOf(vehicleType)));
            }
            if(mediaType!=null) {
                sectionBillQuery.setMediumType(String.valueOf(mediaType));
                System.out.println("mediaType:"+String.valueOf(mediaType));
            }
            if(vehicleClass!=null) {
                sectionBillQuery.setVehicleType(Integer.parseInt(String.valueOf(vehicleClass)));
                System.out.println("vehicleClass:"+Integer.parseInt(String.valueOf(vehicleClass)));
            }
            if(axleCount!=null) {
                sectionBillQuery.setActualAxleNum(Integer.parseInt(String.valueOf(axleCount)));
                System.out.println("axleCount:"+Integer.parseInt(String.valueOf(axleCount)));
            }

        }
    }

}
